#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>

#include "leak.h"
#include "gs.h"
#include "tank.h"

/* leak report format 
 * 代码 罐号 开始日期 开始时间 持续时间 泄漏量 结果 
 */

static pthread_t p_leak;
static int is_leak = 0;

static FILE* add_new_file(const char *filename)
{
    FILE* fp;
    /*
     * Check current directory filelist, if the list more than 1000 delete oldest
     */

    fp = fopen(filename, "a+");

    return fp;
}

/*
 * param mode: same as access
 */
static FILE* gs_leak_get_fp(int mode)
{
    time_t now_t;
    struct tm* tm;
    FILE *fp;
    char filename[128];

    time(&now_t);
    tm = localtime(&now_t);

    /* Get current date as filename, it's format is code_alarm_yyyymmdd.txt */
    sprintf(filename, "./leak/%s_leak_%4d%02d%02d.txt", 
            g_gs.code, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);

    /* Check file exist in ./alarm directory 
     * if exist, open and return it, otherwize, add new file
     */
    if (access(filename, mode) == 0) {
        if (mode == R_OK)
            fp = fopen(filename, "r");
        else if (mode == W_OK)
            fp = fopen(filename, "a");
    }
    else {
        fp = add_new_file(filename);
    }

    return fp;
}

static void gs_leak_save_file(void)
{
    int i;
    GS_TANK *tank;
    struct tm *tm;
    char * stat;
    FILE *fp = NULL;
    double vol;

    tm = localtime(&tank->begin_time);

    fp = gs_leak_get_fp(W_OK);

    if (fp == NULL)
        return;

    for (i=0;i<g_gs.tanks_nr;i++) {
        tank = &g_gs.tanks[i];
        vol = gs_tank_get_volume(tank, tank->end_oil) - gs_tank_get_volume(tank, tank->begin_oil);
        if (vol > g_gs.leak_value)
            stat = "渗漏";
        else
            stat = "正常";

        fprintf(fp, "%s\t" // Code
                "%d\t" // Tank ID
                "%04d%02d%02d\t" // Date
                "%02d%02d%02d\t" // tm
                "%d\t" // lenght
                "%.2f\t" // value
                "%s\n", // stat
                g_gs.code,
                tank->tank_id,
                tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
                tm->tm_hour, tm->tm_min, tm->tm_sec,
                g_gs.leak_time,
                vol,
                stat);
    }

    if (fp)
        fclose (fp);
}
static void* leak_loop(void* add_data)
{
    int i;
    int lt;
    GS_TANK *tank;

    lt = g_gs.leak_time;
    
    for (i=0;i<g_gs.tanks_nr;i++) {
        tank = &g_gs.tanks[i];
        tank->begin_oil = tank->oil;
        tank->state = 3;
    }

    sleep(lt);

    for (i=0;i<g_gs.tanks_nr;i++) {
        tank = &g_gs.tanks[i];
        tank->end_oil = tank->oil;
        tank->state = 3;
    }

      
    gs_leak_save_file();

    is_leak = 0;

    return NULL;
}

int gs_leak_ftp(void)
{
    /* Get new file from alarm index file */

    /* Send file to ftp server */

    return 0;
}

int gs_leak_init(void)
{
    // TODO
    mkdir ("leak", 00777); 
    return 0;
}

int gs_leak_save()
{

    if (is_leak != 0)
        return 1; // running

    
    pthread_create (&p_leak, NULL, leak_loop, NULL);

    return 0;
}
